package com.heatingcharge.controller.sys;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heatingcharge.common.util.R;
import com.heatingcharge.dto.SysUserDTO;
import com.heatingcharge.param.SysUserParam;
import com.heatingcharge.security.JwtUser;
import com.heatingcharge.security.util.SecurityUtil;
import com.heatingcharge.service.sys.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 后台管理用户  前端控制器
 * </p>
 *
 * @author devin.jiang
 * @since 2019-06-26
 */
@Api(value = "用户模块")
@RestController
@RequestMapping("/sys-user")
public class SysUserController {
    @Autowired
    ISysUserService sysUserService;

    @ApiOperation(value = "获取当前登录的用户信息",notes = "获取当前登录的用户信息" )
    @GetMapping("/current")
    public R getCurrentUser(){
         JwtUser jwtUser=SecurityUtil.getUser();
        return R.ok(jwtUser);
    }

    @GetMapping
    public R getSysUsers(Page page, SysUserDTO sysUserDTO){
        IPage<SysUserDTO> sysUserDTOIPage=sysUserService.getSysUsers(page,sysUserDTO);
        return R.ok(sysUserDTOIPage);
    }

    @PostMapping
    public R saveSysUser(@RequestBody SysUserParam sysUserParam){
        if (sysUserService.save(sysUserParam)){
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation(value = "更新用户",notes = "更新用户")
    @PutMapping
    public R updateSysUser(@RequestBody SysUserParam sysUserParam){
        if (sysUserService.update(sysUserParam)){
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation(value = "删除用户",notes = "根据主键ID删除用户")
    @DeleteMapping("/{id}")
    public R delSysUser(@PathVariable("id") Integer id){
        if (sysUserService.removeById(id)){
            return R.ok();
        }
        return R.error();
    }
}